package com.example.springboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot.common.Result;
import com.example.springboot.entity.Book;
import com.example.springboot.service.BookService;
import com.example.springboot.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookService bookService;

    /**
     * 新增
     */
    @PostMapping
    public Result<?> save(@RequestBody Book book){
        bookService.insert(book);
        return Result.success();
    }

    /**
     * 删除
     */
    @DeleteMapping("/{id}")
    public Result<?> remove(@PathVariable Integer id){
        bookService.delete(id);
        return Result.success();
    }

    /**
     * 更新
     */
    @PutMapping
    public Result<?> update(@RequestBody Book book){
        bookService.update(book);
        return Result.success();
    }

    /**
     * 查询
     * @param pageNum 页数
     * @param pageSize 每页多少条
     * @param search 查询关键字
     */
    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search){
        Page<Book> bookPage = bookService.select(pageNum, pageSize, search);
        return Result.success(bookPage);
    }
}
